package com.feihong.txgw.tx_precenter;

import com.feihong.txgw.inter.contract.product.IProSearchContract;
import com.feihong.txgw.model.bean.searchList.SearchListEntity;
import com.feihong.txgw.model.bean.searchList.SearchPhoneListModel;
import com.feihong.txgw.tx_model.ProSearchMainModel;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.feihong.txgw.ui.activity.product.SearchActivity.goodsType;
import static com.feihong.txgw.ui.activity.product.SearchActivity.storeOnePageCount;
import static com.feihong.txgw.ui.activity.product.SearchActivity.storeType;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ProSearcMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/8 11:21
 * @Describe
 * @Change
 */
public class ProSearcMainPrecenter extends IProSearchContract.AbProSearchMainPrecenter {


    private int mCurrentPageNum = 1;
    private String mRequestType = goodsType;
    private List<SearchListEntity> dataList;
    /**
     * 是否拦截上拉加载
     */
    private boolean isInterceptLoad = false;
    @Override
    public void getGoodsData(final String lat, final String lng, final String keyWord) {
        mIModel.getGoodsData(lat, lng, keyWord, mRequestType, mCurrentPageNum).subscribe(new Observer<SearchPhoneListModel>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(SearchPhoneListModel searchPhoneListModel) {
                /**
                 *  if(如果返回的数据为200 ){
                 *     if(当前返回的数据类型是商品类型 ){
                 *       if(并且返回的数据不为空){
                 *          if(门店类型的数据小于一页的数据量  则不拦截上拉加载  并且 下次加载商品类型的数据  ){
                 *              禁止上拉加载
                 *          }
                 *          设置数据到view
                 *       }else{
                 *           设置无数据
                 *       }
                 *    }else{
                 *          返回门店类型的数据
                 *        if(返回的数据不为空){
                 *            if(数据数量小于一页的数量){
                 *                设置下次请求类型为商品类型
                 *            }
                 *            设置到view
                 *        }else{
                 *            直接请求商品类型的数据
                 *        }
                 *    }
                 *  }else{
                 *      设置界面无数据
                 *  }
                 *
                 */
                if (searchPhoneListModel.getCode() == 200) {
                    dataList = new ArrayList<>();
                    // 如果是商品类型
                    if (mRequestType.equals(goodsType)) {
                        List<SearchPhoneListModel.DataBean.GoodsListBean> goodsList = searchPhoneListModel.getData().getGoods_list();
                        if (goodsList != null && goodsList.size() != 0) {
                            mCurrentPageNum ++;
                            dataList.addAll(goodsList);
                            if(dataList.size() < storeOnePageCount){
                                isInterceptLoad = true;
                            }
                            mIView.getDataResult(dataList,isInterceptLoad);
                        } else {
                            mIView.noData();
                        }
                    } else {
                        // 如果是门店类型
                        List<SearchPhoneListModel.DataBean.StoreListBean> storeList = searchPhoneListModel.getData().getStore_list();
                        if (storeList != null && storeList.size() != 0) {
                            dataList.addAll(storeList);
                            if (storeList.size() > storeOnePageCount) {
                                mCurrentPageNum++;
                                mIView.getDataResult(dataList,false);
                            } else {
                                mRequestType = goodsType;
                                mCurrentPageNum = 1;
                                mIView.getDataResult(dataList,false);
                            }
                        } else {
                            mRequestType = goodsType;
                            mCurrentPageNum = 1;
                            getGoodsData(lat, lng, keyWord);
                        }
                    }
                } else {
                    mIView.noData();
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void getGoodsForFirst(String lat, String lng, String keyWord) {
        mCurrentPageNum = 1;
        mRequestType = storeType;
        isInterceptLoad = false;
        getGoodsData(lat, lng, keyWord);
    }

    @Override
    public IProSearchContract.IProSearchMainModel getModel() {
        return ProSearchMainModel.create();
    }

    public static ProSearcMainPrecenter create() {
        return new ProSearcMainPrecenter();
    }

    @Override
    public void onStart() {

    }
}
